//
//  YXMachinePopView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/24.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXMachinePopView.h"
#import "YXNumberButton.h"
#import "YXMachinesModel.h"
@interface YXMachinePopView ()


@property (weak, nonatomic) IBOutlet YXNumberButton *numberView;
@property (weak, nonatomic) IBOutlet UIImageView *picImageView;
@property (weak, nonatomic) IBOutlet UILabel *addressLab;
@property (strong, nonatomic) IBOutlet UIButton *confirmBtn;
@property (nonatomic ,strong) NSString *num;
@property (nonatomic ,assign) CGFloat totalPrice;
@property (nonatomic ,strong) NSString *totalPriceStr;
@property (nonatomic ,strong) NSString *goodID;
@end

@implementation YXMachinePopView

- (void)setModel:(YXMachinesModel *)model {
    _model = model;
    [_picImageView sd_setImageWithURL:[NSURL URLWithString:_model.coverImgUrl] placeholderImage:[UIImage imageNamed:@""]];
}

- (void)setAddress:(NSString *)address {
    _address = address;
    _addressLab.text = [NSString stringWithFormat:@"收货地址:%@",address];
}

-(instancetype)initWithFrame:(CGRect)frame withDataArray:(NSArray *)dataArray{
    
    if (self = [super initWithFrame:frame]) {
        // 从xib中找到我们定义的view
        NSArray *viewArray = [[NSBundle mainBundle]loadNibNamed:@"YXMachinePopView" owner:self options:nil];
        self = viewArray[0];
        self.frame = frame;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        
        self.confirmBtn.layer.masksToBounds = YES;
        self.confirmBtn.layer.cornerRadius = 20.0f;
        [self.numberView setImageWithincreaseImage:[UIImage imageNamed:@"+"] decreaseImage:[UIImage imageNamed:@"-"]];
   
        self.num = @"1";
        
        self.numberView.numberBlock = ^(NSString *num) {
            self.num = num;
            self.totalPrice = [self.num floatValue] * [_model.unitPrice floatValue];
            self.goodID = _model.goodsId;
            self.totalPriceStr = [NSString stringWithFormat:@"%.2f",self.totalPrice];
        };
    }
    return self;
    
}

- (IBAction)changeAddressAction:(UIButton *)sender {
    if (self.clickChangeAddressBtnBlock) {
        self.clickChangeAddressBtnBlock();
    }
}

- (IBAction)confirmBtnAction:(UIButton *)sender {
    self.totalPrice = [self.num floatValue] * [_model.unitPrice floatValue];
    self.totalPriceStr = [NSString stringWithFormat:@"%.2f",self.totalPrice];
    self.clickConfirmBtnBlock(self.num, _address,self.totalPriceStr,_model.goodsId);
    
}




- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    }];
}


@end
